package com.cy.web.action.system;

import java.io.Serializable;
import java.util.LinkedHashMap;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cy.bean.Paging;
import com.cy.bean.system.Blogroll;
import com.cy.service.system.BlogrollService;
import com.cy.utils.SysParam;
import com.cy.web.action.BaseAction;
import com.opensymphony.xwork2.ActionContext;

/**
 * 友情链接控制器
 * 
 * @author CY
 *
 */
@Controller
@Scope("prototype")
public class BlogrollAction extends BaseAction {

	@Resource
	private BlogrollService blogrollService;
	private Blogroll blogroll;

	/**
	 * 友情链接列表
	 * 
	 * @return
	 */
	public String list() {
		Paging<Blogroll> paging = new Paging<Blogroll>(getPage());
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("blogrool_order", "asc");
		paging.setQueryResult(blogrollService.getScrollData(paging.getfirstResult(),paging.getMaxResult(), orderby));
		ActionContext.getContext().put("paging", paging);
		return "list";
	}

	/**
	 * 添加友情链接UI
	 * 
	 * @return
	 */
	public String addUI() {
		return "add";
	}

	/**
	 * 添加友情链接
	 * 
	 * @return
	 */
	public String add() {
		try {
			blogrollService.save(blogroll);
			ActionContext.getContext().put("message", "超链接添加成功");
		} catch(Exception e) {
			e.printStackTrace();
			ActionContext.getContext().put("message", "超链接添加失败");
		}
		ActionContext.getContext().put("url", SysParam.read("blogrollAdd"));
		return "dialog";
	}

	/**
	 * 删除友情链接
	 * @return
	 */
	public String del() {
		try {
			blogrollService.delete((Serializable[])getIds());
			ActionContext.getContext().put("message", "超链接删除成功");
		} catch(Exception e) {
			ActionContext.getContext().put("message", "超链接删除失败");
		}
		ActionContext.getContext().put("url", SysParam.read("blogrollList"));
		return "dialog";
	}
	
	/**
	 * 修改友情链接UI
	 * @return
	 */
	public String editUI() {
		blogroll = blogrollService.find(getId());
		return "edit";
	}
	
	/**
	 * 修改友情链接
	 * @return
	 */
	public String edit() {
		try {
			blogrollService.update(blogroll);
			ActionContext.getContext().put("message", "超链接修改成功");
		} catch(Exception e) {
			ActionContext.getContext().put("message", "超链接修改失败");
		}
		ActionContext.getContext().put("url", SysParam.read("blogrollList"));
		return "dialog";
	}
	
	public Blogroll getBlogroll() {
		return blogroll;
	}

	public void setBlogroll(Blogroll blogroll) {
		this.blogroll = blogroll;
	}

}
